package xin.alum.aim;

import io.netty.handler.codec.http.FullHttpResponse;
import xin.alum.aim.constant.ChannelPlatform;
import xin.alum.aim.groups.SessionGroups;
import xin.alum.aim.groups.Sessions;
import xin.alum.aim.handler.AimReceiver;
import io.netty.channel.Channel;
import io.netty.handler.codec.http.FullHttpRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;

/**
 * @auther Alum(alum @ live.cn)
 * @date 2021/8/7 9:53 1231321
 */
@Component
public class AimReciveService extends AimReceiver {

    @Autowired
    private Sessions sessions;

    @Autowired
    private SessionGroups groups;

    @Override
    public boolean onHandShake(Channel ch, FullHttpRequest req, FullHttpResponse res) {
        String token = ch.id().toString();
        ArrayList<String> tokens = (ArrayList<String>) sessions.getAttr(ch, "token");
        if (tokens != null) {
            token = tokens.get(0);
        }
        return super.onHandShake(ch, req, res);
    }

    @Override
    public void onHandShaked(Channel ch) {
        String token = ch.id().toString();
        sessions.bindUser(ch, token, ChannelPlatform.WINDOWS, "");
    }
}
